package com.airtight.util;


import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;

import static android.opengl.GLES20.GL_LINEAR;
import static android.opengl.GLES20.GL_LINEAR_MIPMAP_LINEAR;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER;
import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGenTextures;
import static android.opengl.GLES20.glGenerateMipmap;
import static android.opengl.GLES20.glTexParameteri;
import static android.opengl.GLUtils.texImage2D;

public class TextureHelper {

    /**turns an image into a texture*/
    public static int loadTexture(Context context, int resourceId) {
        final int[] textureObjectIds = new int[1];

        glGenTextures(1, textureObjectIds, 0);          //creates a texture object

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]); //future texture calls are called on this object

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);     //minification
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);                   //magnification

        texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);        //bitmap to texture object
        bitmap.recycle();                               //releases bitmap
        glGenerateMipmap(GL_TEXTURE_2D);                //generates mipmaps out of texture
        glBindTexture(GL_TEXTURE_2D, 0);        //unbind this object from future texture calls
        return textureObjectIds[0];
    }

    public static int loadTextureAsset(Context context, String fileName) {
        final int[] textureObjectIds = new int[1];
        glGenTextures(1, textureObjectIds, 0);          //creates a texture object

        AssetManager assets = context.getAssets();
        InputStream in;
        Bitmap bitmap = null;

        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]); //future texture calls are called on this object

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);     //minification
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);                   //magnification

        texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);        //bitmap to texture object
        assert bitmap != null;
        bitmap.recycle();                               //releases bitmap
        glGenerateMipmap(GL_TEXTURE_2D);                //generates mipmaps out of texture
        glBindTexture(GL_TEXTURE_2D, 0);        //unbind this object from future texture calls
        return textureObjectIds[0];
    }
}
